package com.ssm.service.imp;

import com.github.pagehelper.PageHelper;
import com.ssm.mapper.CustomerMapper;
import com.ssm.mapper.ProductMapper;
import com.ssm.mapper.ProductTypeMapper;
import com.ssm.pojo.Product;
import com.ssm.pojo.ProductType;
import com.ssm.service.ProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class ProductServiceImp implements ProductService {

    @Autowired
    private ProductMapper productMapper;

    @Autowired
    private ProductTypeMapper productTypeMapper;


    public void insert(Product product) {
        productMapper.insert(product);
    }

    public void update(Product product) {
        productMapper.update(product);
    }

    public void delete(String NO) {
        productMapper.delete(NO);
    }

    public List<Product> selectProductsPage(Integer pageNum, Integer pageSize, String NO) {
        if(pageNum!=null && pageSize!=null){
            PageHelper.startPage(pageNum, pageSize);
        }
        List<Product> products =productMapper.selectProductsPage(NO);
        if(products!=null && products.size()>0){
            for(Product product:products){
                ProductType type = productTypeMapper.selectByNO(product.getProducttypeNO());
                product.setProductType(type);
            }
        }
        return products;
    }

    public Integer countSize(String NO) {
        Integer size= productMapper.countSize(NO);
        return size;
    }

    public Product selectProductByNO(String NO) {
        Product product = productMapper.selectByNO(NO);
        if(product!=null){
            ProductType type = productTypeMapper.selectByNO(product.getProducttypeNO());
            product.setProductType(type);
        }
        return product;
    }
}
